import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Array;
import java.util.StringTokenizer;


public class Learn 
{
	static int[] ans=new int[26];
	static char c;
	
	private static void processString(String inputString){
		inputString=inputString.toLowerCase();
		inputString=inputString.replace(" ", "");
		for (int i=0;i<inputString.length();i++){
			c=inputString.charAt(i);
			ans[c-'a']++;
		}
	}
	
	public static void main(String[] args)
	{
		System.out.println(System.getProperties().getProperty("user.dir"));
		
		String inputFile = args[0];
		//String inputFile = ".\\src\\Example2\\input2-2.txt";
		String outputFile = args[1];
		//String outputFile=".\\src\\Example2\\input2-2_processed.txt";
		
		read_From_File(inputFile);
		
		write_To_File(outputFile);
		

		
		//TODO
		/*
		 *  Here you should call your
		 *  learning code which will read the InputFile
		 *  and will write the frequencies
		 *  to the OutputFile
		 *  
		 */
		
	}
	
	private static void write_To_File(String outputFilename) {

        System.out.println("Writing to file " + outputFilename);
        
        
        try {
            File outFile = new File(outputFilename);
            FileWriter ofw = new FileWriter(outFile);

            //Writing to file
            for (int i=0;i<ans.length;i++){
    			if (ans[i]>0){
    				c=(char) (i+'a');
    				String s=c+":"+ans[i];
    				ofw.write(s+"\r\n");
    				
    				System.out.println(c+":"+ans[i]);
    			}
    		}
          	
            ofw.close();
        }
        catch (Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+outputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }

    }
	public static void read_From_File(String inputFilename){
       	System.out.println("Reading from file " + inputFilename);
       
       	try {
            File inFile = new File(inputFilename);
            FileReader ifr = new FileReader(inFile);
            BufferedReader ibr= new BufferedReader(ifr) ;

            String line = "" ;
            while (line != null )
            {
                line = ibr.readLine() ;
                if (line != null)
                {
                    StringTokenizer st = new StringTokenizer(line); // splits the line into tokens
					
                    while(st.hasMoreTokens())
                    {
                    	String token = st.nextToken().trim();
                    	System.out.println(token);
                    	processString(token);
                    }                   
                }
            }

            ibr.close();
            ifr.close();
        }

        catch(Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+inputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }

    }

}
